//
//  CRHeaderView.m
//  CRChatRoom
//
//  Created by lanou3g on 15/11/19.
//  Copyright © 2015年 SecondGroup. All rights reserved.
//

#import "CRHeaderView.h"
#import "CRButton.h"
// 距离左右间距
#define margin 10


@interface CRHeaderView ()
/**
 *  搜索框
 */
@property (nonatomic, strong) UISearchBar *searchBar;

/**
 *  好友动态
 */
@property (nonatomic, strong) CRButton *friendButton;

/**
 *  附近
 */
@property (nonatomic, strong) CRButton *nearButton;

/**
 *  兴趣部落
 */
@property (nonatomic, strong) CRButton *hobbyButton;

@end

@implementation CRHeaderView


- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        // 搜索框
        self.searchBar = [[UISearchBar alloc] init];
        self.searchBar.placeholder = @"搜索";
        self.searchBar.backgroundColor = [UIColor clearColor];
        
        // 好友动态
        self.friendButton = [[CRButton alloc] init];
        [self.friendButton setImage:[UIImage imageNamed:@"iconfont-kongjian"] forState:UIControlStateNormal];
        [self.friendButton setTitle:@"好友动态" forState:UIControlStateNormal];

        [self.friendButton addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
        self.friendButton.tag = ButtonTypeFriend;

        
        
        // 附近
        self.nearButton = [[CRButton alloc] init];
        [self.nearButton setImage:[UIImage imageNamed:@"iconfont-tongcheng"] forState:UIControlStateNormal];
        [self.nearButton setTitle:@"附近" forState:UIControlStateNormal];
        
        [self.nearButton addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
        self.nearButton.tag = ButtonTypeNear;

        
        // 兴趣部落
        self.hobbyButton = [[CRButton alloc] init];
        [self.hobbyButton setImage:[UIImage imageNamed:@"iconfont-xingqu"] forState:UIControlStateNormal];
        [self.hobbyButton setTitle:@"兴趣部落" forState:UIControlStateNormal];
    
        [self.hobbyButton addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
        self.hobbyButton.tag = ButtonTypeHobby;

        
        [self addSubview:self.searchBar];
        [self addSubview:self.friendButton];
        [self addSubview:self.hobbyButton];
        [self addSubview:self.nearButton];
        
        
        
    }
    return self;
}

// 设置子控件的frame
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    // self 的宽度 --- cell的宽度
    CGFloat viewWith = self.frame.size.width;
    
    self.searchBar.frame = CGRectMake(margin, margin, viewWith - 2 * margin, 30);
    
    CGFloat with = self.frame.size.width / 3;

    self.friendButton.frame = CGRectMake(0, CGRectGetMaxY(self.searchBar.frame) + margin + 5, with, 60);
    self.nearButton.frame = CGRectMake(with * 1, CGRectGetMaxY(self.searchBar.frame) + margin + 5, with, 60);
    self.hobbyButton.frame = CGRectMake(with * 2, CGRectGetMaxY(self.searchBar.frame) + margin + 5, with, 60);

    
}

- (void)clickButton:(CRButton *)button
{
    
    if ([self.delegate respondsToSelector:@selector(didClickButton:)]) {
        [self.delegate didClickButton:(int)button.tag];
    }
}



@end









